package the.broccoli.uc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.util.TreeUtils;
import org.wisteria.framework.web.mvc.constant.WebConsts;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.broccoli.uc.constant.RoleType;
import the.broccoli.uc.dto.RoleCondition;
import the.broccoli.uc.dto.RoleTree;
import the.broccoli.uc.entity.Role;
import the.broccoli.uc.mapper.RoleMapper;
import the.broccoli.uc.service.IRoleService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 */
@Service
public class RoleServiceImpl extends AbstractSuperService<RoleMapper, Role> implements IRoleService {

    /**
     * 是否管理员类型的角色
     *
     * @param roleId
     * @return
     */
    @Override
    public boolean isAdmin(String roleId) {
        Role role = getById(roleId);
        if (role != null && RoleType.admin.name().equals(role.getRoleType())) {
            return true;
        }
        return false;
    }

    /**
     * 是否包含管理员类型的角色
     *
     * @param roleIdList
     * @return
     */
    @Override
    public boolean isAdmin(List<String> roleIdList) {
        if (CollectionUtils.isEmpty(roleIdList)) {
            return false;
        }

        List<Role> roleList = getRoleListByRoleIdList(roleIdList);
        if (CollectionUtils.isEmpty(roleList)) {
            return false;
        }

        for (Role role : roleList) {
            if (RoleType.admin.name().equals(role.getRoleType())) {
                return true;
            }
        }

        return false;
    }

    /**
     * 获取角色树
     *
     * @param roleIdList
     * @return
     */
    @Override
    public List<RoleTree> getTree(List<String> roleIdList) {
        return buildTree(getRoleListByRoleIdList(roleIdList));
    }

    /**
     * 获取角色树
     *
     * @param roleIdList
     * @param rootRoleIdList
     * @return
     */
    @Override
    public List<RoleTree> getTree(List<String> roleIdList, List<String> rootRoleIdList) {
        return buildTree(getRoleListByRoleIdList(roleIdList), rootRoleIdList);
    }

    @Override
    public Role addAndGetTenantAdmin(Integer tenantId, String operatorId, String operatorName) {
        List<Role> roleList = baseMapper.selectList(new LambdaQueryWrapper<Role>()
                .eq(Role::getTenantId, tenantId)
                .eq(Role::getRoleType, RoleType.TENANT_ADMIN.name())
        );
        if (CollectionUtils.isEmpty(roleList)) {
            // 不存在租户的管理员角色，则创建一个新的
            Role role = new Role();
            role.setTenantId(tenantId);
            role.setRoleName("超级管理员");
            role.setRoleType(RoleType.TENANT_ADMIN.name());
            role.setDisabled(0);
            baseMapper.insert(role);
            return role;
        }

        return roleList.get(0);
    }

    /**
     * 获取租户管理员角色
     *
     * @param tenantId 租户ID
     * @return 角色
     */
    @Override
    public Role getTenantAdmin(Integer tenantId) {
        List<Role> roleList = baseMapper.selectList(new LambdaQueryWrapper<Role>()
                .eq(Role::getTenantId, tenantId)
                .eq(Role::getRoleType, RoleType.TENANT_ADMIN.name())
        );
        return CollectionUtils.isEmpty(roleList) ? null : roleList.get(0);
    }

    /**
     * 创建角色树
     *
     * @param roleList
     * @return
     */
    private List<RoleTree> buildTree(List<Role> roleList) {
        List<RoleTree> treeList = roleList.stream()
                .filter(entity -> !entity.getRoleId().equals(entity.getParentId()))
                .map(entity -> {
                    RoleTree node = new RoleTree();
                    node.setId(entity.getRoleId());
                    node.setParentId(entity.getParentId());
                    node.setName(entity.getRoleName());
                    return node;
                }).collect(Collectors.toList());
        return TreeUtils.bulid(treeList, WebConsts.ROOT);
    }

    /**
     * 创建角色树
     *
     * @param roleList
     * @return
     */
    private List buildTree(List<Role> roleList, List<String> rootRoleIdList) {
        List<RoleTree> treeList = roleList.stream()
                .filter(entity -> !entity.getRoleId().equals(entity.getParentId()))
                .map(entity -> {
                    RoleTree node = new RoleTree();
                    node.setId(entity.getRoleId());
                    node.setParentId(entity.getParentId());
                    node.setName(entity.getRoleName());
                    return node;
                }).collect(Collectors.toList());
        return TreeUtils.bulids(treeList, rootRoleIdList);
    }

    /**
     * 获取角色列表
     * @param roleIdList    角色ID列表
     * @return
     */
    private List<Role> getRoleListByRoleIdList(List<String> roleIdList) {
        RoleCondition condition = new RoleCondition();
        condition.setRoleIdList(roleIdList);
        return listByCondition(condition);
    }

    @Override
    public List<String> getRoleIdList(List<String> parentRoleIdList) {
        if (CollectionUtils.isEmpty(parentRoleIdList)) {
            return null;
        }
        List<Role> roleList = baseMapper.selectList(new LambdaQueryWrapper<Role>()
                .in(Role::getParentId, parentRoleIdList)
        );

        return CollectionUtils.isEmpty(roleList) ? null : roleList.stream().map(Role::getRoleId).collect(Collectors.toList());
    }
}
